package com.SDU.policedispatch.controller.schedule;

import com.SDU.policedispatch.controller.schedule.utils.*;

import com.SDU.policedispatch.entity.ScheduleHistory;
import com.SDU.policedispatch.entity.Schedules;
import com.SDU.policedispatch.entity.PersonnelInfo;

import com.SDU.policedispatch.service.schedule.DutyListService;
import com.SDU.policedispatch.service.schedule.PersonnelInfoService;
import com.SDU.policedispatch.service.schedule.ScheduleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.*;

@RestController
public class ScheduleServiceController {
    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private DutyListService dutyListService;

    @Autowired
    private PersonnelInfoService personnelInfoService;

    @GetMapping("/api/get/history_schedule")
    public List<ScheduleHistory> getScheduleByDate(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        return scheduleService.GetHistorySchedule(date);
    }

    @GetMapping("/api/get/schedule")
    List<Schedules> getSchedule() {
        return scheduleService.GetSchedules();
    }

    @GetMapping("/api/personInfo/{id}")
    public PersonInfo getPersonInfo(@PathVariable String id) {
        // 获取PersonnelInfo对象
        PersonnelInfo personnelInfo = personnelInfoService.getPersonnelInfoById(id);

        // 创建一个新的PersonInfo对象，并将PersonnelInfo对象的属性复制到PersonInfo对象中
        PersonInfo personInfo = new PersonInfo();
        personInfo.setId(personnelInfo.getId());
        personInfo.setName(personnelInfo.getName());
        personInfo.setSex(personnelInfo.getSex());
        personInfo.setPoliceType(personnelInfo.getPoliceType());
        personInfo.setPhoneNumber(personnelInfo.getPhoneNumber());

        // 返回PersonInfo对象
        return personInfo;
    }

    @GetMapping("/api/duty-list/{date}")
    public ResponseEntity<DutyList> getDutyList(@PathVariable String date) {
        List<Schedules> schedules = dutyListService.getSchedulesByDate(date);
        System.out.println(schedules);

        DutyList dutyList = new DutyList();
        // set responseLevel based on your logic

        // process center data
        Center center = new Center();
        // set center's fields based on your logic

        // process department data
        Map<String, Department> departmentMap = new HashMap<>();
        for (Schedules schedule : schedules) {
            String departmentName = schedule.getDepartmentName();
            Department department = departmentMap.get(departmentName);
            if (department == null) {
                department = new Department();
                department.setDepartmentName(departmentName);
                department.setDutyLeaders(new ArrayList<>());
                department.setDutyStaff(new ArrayList<>());
                department.setAlternateStaff(new ArrayList<>());
                departmentMap.put(departmentName, department);
            }
            String leader = schedule.getLeader();

            Person person = new Person();
            person.setId(schedule.getId());
            person.setName(schedule.getName());

            if(leader == null) {

            } else if(leader.contains("值班领导")) {
                center.getDutyLeaders().add(person);
            } else if(leader.contains("调度会参与领导")) {
                center.getDispatchLeaders().add(person);
            } else if(leader.contains("指挥中心值班")) {
                center.getCommandCenterDuty().add(person);
            } else if(leader.contains("总指挥长")) {
                center.getSituationCenter().getCommanderInChief().add(person);
            } else if(leader.contains("专职指挥长")) {
                center.getSituationCenter().getProfessionalCommander().add(person);
            } else if(leader.contains("接警班组")) {
                center.getSituationCenter().getAlarmTeam().add(person);
            }

            String word = schedule.getMainOrAlternate();
            if(word == null) {

            } else if (word.contains("值班领导")) {
                department.getDutyLeaders().add(person);
            } else if (word.contains("主班")) {
                department.getDutyStaff().add(person);
            } else if (word.contains("副班")) {
                department.getAlternateStaff().add(person);
            }
        }

        List<Department> departments = new ArrayList<>(departmentMap.values());
        dutyList.setDepartments(departments);
        dutyList.setCenter(center);

        return ResponseEntity.ok(dutyList);
    }
}
